package com.aspire.moss.portal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.aspire.moss.portal.R;
import com.aspire.moss.portal.view.Workspace.WorkspaceListener;

public class ClipImageView extends View implements WorkspaceListener {

	private Bitmap clipnofocus;
	private Bitmap clipfocus;
	private int  count ;//有多少个
	Context mContext;
	
	private int mSelect;//哪个选中
	
	public ClipImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs , R.styleable.ClipImageView);
		clipfocus = ((BitmapDrawable)a.getDrawable(R.styleable.ClipImageView_clipfocus)).getBitmap();
		clipnofocus = ((BitmapDrawable)a.getDrawable(R.styleable.ClipImageView_clipnofocus)).getBitmap();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(count * clipnofocus.getWidth(), clipnofocus.getHeight());
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int left = 0 ;
		//drawnofocus
		for(int i = 0 ; i < count ; i++){
			left = i *  clipnofocus.getWidth();
			if(mSelect == i){
				canvas.drawBitmap(clipfocus, left, 0, null);
			}else{
				canvas.drawBitmap(clipnofocus, left, 0, null);
			}
		}
		canvas.save();
	}

	public void onSelectChange(int select){
		if(this.mSelect != select){
			this.mSelect = select;
			postInvalidate();
		}
	}
	
	interface ClipImageViewOnSelectChang{
		void onScreenChange(int screen);
		void onSelectChamge(int select);
	}

	@Override
	public void onSelectScreenChange(int mCurrentScreen) {
		onSelectChange(mCurrentScreen);
	}

	@Override
	public void onScreenCountChange(int count) {
		this.count = count;
		postInvalidate();
	}
}
